
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;

/*
 * frmMain.java
 *
 * Created on 14 April 2010, 16:53
 */
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.Timer;


/**
 *
 * @author  Administrator
 */
public class frmMain extends javax.swing.JFrame {
    int _width,_height,Totaltime;
    int distanceH=25,distanceW=31,StartTime=0;
    boolean Login=false;
    int ID,mark;
    String name="";
    String[][] w=new String[2][];
    Timer t=new Timer(20, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(pnlM.getWidth()==_width*distanceW+10 && pnlM.getHeight()==_height*distanceH+10)
                t.stop();
            else
                changeDimention();
            //JOptionPane.showConfirmDialog(null, "asdfasf");
        }
    });
    Timer t1=new Timer(1000,new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            StartTime++;
            String temp="";
            temp+=((StartTime/360<10)?"0"+StartTime/360:StartTime/360)+":"+
                    ((StartTime/60<10)?"0"+StartTime/60:StartTime/60)+":"+
                    ((StartTime%60<10)?"0"+StartTime%60:StartTime%60)+":"+" phút";
            txtTime.setText(temp);
        }
    });
    private String tempStr;
    private int magin=7;
    /** Creates new form frmMain */
    public frmMain() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane3 = new javax.swing.JScrollPane();
        txtMain = new javax.swing.JTextArea();
        lblGY = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        pnl6 = new javax.swing.JPanel();
        pnlIdH = new javax.swing.JPanel();
        pnlM = new javax.swing.JPanel();
        pnlIdV = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        lblType = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        lblTitle = new javax.swing.JLabel();
        pnlTime = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblStartTime = new javax.swing.JLabel();
        txtTime = new javax.swing.JTextField();
        btnNewGame = new javax.swing.JButton();
        btnEndGame = new javax.swing.JButton();
        btnPublic = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mnuAddQ = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        mnuAbout = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        txtMain.setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background"));
        txtMain.setColumns(20);
        txtMain.setEditable(false);
        txtMain.setRows(5);
        jScrollPane3.setViewportView(txtMain);

        lblGY.setText("Gợi ý:");

        jScrollPane1.setAutoscrolls(true);

        pnl6.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnl6.setMaximumSize(new java.awt.Dimension(350, 300));
        pnl6.setPreferredSize(new java.awt.Dimension(500, 400));
        pnl6.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        pnlIdH.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.UIManager.getDefaults().getColor("Panel.background"), javax.swing.UIManager.getDefaults().getColor("Panel.background")));

        org.jdesktop.layout.GroupLayout pnlIdHLayout = new org.jdesktop.layout.GroupLayout(pnlIdH);
        pnlIdH.setLayout(pnlIdHLayout);
        pnlIdHLayout.setHorizontalGroup(
            pnlIdHLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 26, Short.MAX_VALUE)
        );
        pnlIdHLayout.setVerticalGroup(
            pnlIdHLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 156, Short.MAX_VALUE)
        );

        pnl6.add(pnlIdH, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 30, 160));

        pnlM.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.UIManager.getDefaults().getColor("Panel.background"), javax.swing.UIManager.getDefaults().getColor("Panel.background")));
        pnlM.setAutoscrolls(true);

        org.jdesktop.layout.GroupLayout pnlMLayout = new org.jdesktop.layout.GroupLayout(pnlM);
        pnlM.setLayout(pnlMLayout);
        pnlMLayout.setHorizontalGroup(
            pnlMLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 196, Short.MAX_VALUE)
        );
        pnlMLayout.setVerticalGroup(
            pnlMLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 156, Short.MAX_VALUE)
        );

        pnl6.add(pnlM, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 40, 200, 160));

        pnlIdV.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.UIManager.getDefaults().getColor("Panel.background"), javax.swing.UIManager.getDefaults().getColor("Panel.background")));
        pnlIdV.setAutoscrolls(true);

        org.jdesktop.layout.GroupLayout pnlIdVLayout = new org.jdesktop.layout.GroupLayout(pnlIdV);
        pnlIdV.setLayout(pnlIdVLayout);
        pnlIdVLayout.setHorizontalGroup(
            pnlIdVLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 196, Short.MAX_VALUE)
        );
        pnlIdVLayout.setVerticalGroup(
            pnlIdVLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 16, Short.MAX_VALUE)
        );

        pnl6.add(pnlIdV, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 20, 200, 20));

        jScrollPane1.setViewportView(pnl6);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin game.")));

        jLabel2.setText("Loại game: ");

        jLabel6.setText("Tiêu đề:");

        pnlTime.setEnabled(false);

        jLabel1.setText("Thời gian bắt đầu:");

        jLabel4.setText("Thời gian đã chơii:");

        txtTime.setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background"));
        txtTime.setEditable(false);
        txtTime.setFont(new java.awt.Font("Tahoma", 1, 11));
        txtTime.setBorder(null);

        btnNewGame.setText("Chọn game");
        btnNewGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewGameActionPerformed(evt);
            }
        });

        btnEndGame.setText("Kết thúc");
        btnEndGame.setEnabled(false);
        btnEndGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEndGameActionPerformed(evt);
            }
        });

        btnPublic.setText("Lưu tên");
        btnPublic.setEnabled(false);
        btnPublic.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPublicActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout pnlTimeLayout = new org.jdesktop.layout.GroupLayout(pnlTime);
        pnlTime.setLayout(pnlTimeLayout);
        pnlTimeLayout.setHorizontalGroup(
            pnlTimeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlTimeLayout.createSequentialGroup()
                .add(pnlTimeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(pnlTimeLayout.createSequentialGroup()
                        .add(pnlTimeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jLabel4))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(pnlTimeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(lblStartTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 260, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(txtTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 261, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(pnlTimeLayout.createSequentialGroup()
                        .add(49, 49, 49)
                        .add(btnNewGame)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnEndGame)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnPublic)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlTimeLayout.setVerticalGroup(
            pnlTimeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlTimeLayout.createSequentialGroup()
                .add(pnlTimeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(lblStartTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(pnlTimeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(txtTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 19, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(pnlTimeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnNewGame)
                    .add(btnEndGame)
                    .add(btnPublic))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(pnlTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel6)
                            .add(jLabel2))
                        .add(41, 41, 41)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel2Layout.createSequentialGroup()
                                .add(lblTitle, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)
                                .addContainerGap())
                            .add(lblType)))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(lblTitle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(lblType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(pnlTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(125, 125, 125))
        );

        jMenu1.setText("File");
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        mnuAddQ.setText("Thêm câu đố");
        mnuAddQ.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuAddQActionPerformed(evt);
            }
        });
        jMenu1.add(mnuAddQ);

        jMenuItem1.setText("Điểm cao");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Thoát");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu3.setText("Trợ giúp");

        mnuAbout.setText("Thông tin tác giả");
        mnuAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuAboutActionPerformed(evt);
            }
        });
        jMenu3.add(mnuAbout);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 324, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 10, Short.MAX_VALUE)
                        .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 554, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(lblGY))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 305, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(lblGY))
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 211, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void txtKeyReleased(java.awt.event.KeyEvent evt) {
        // TODO add your handling code here:
        JLabel lbl=null;
        JTextField temp=(JTextField)   evt.getComponent();
        int point=Integer.parseInt(temp.getName().substring(3));
        String str="";
        if(temp.getText().length()>1) {
            JOptionPane.showMessageDialog(this, "Các hạ chỉ được oánh 1 chữ thôi."+evt.getComponent().getName());
            temp.setText(tempStr);
        }
        else
        {
            for(int i=0;i<_width;i++)//lấy cho cột
            {
                //JOptionPane.showConfirmDialog(this, _width+"  "+(i*_width)+"   "+(i*_width+point%_height));
                temp=(JTextField)pnlM.getComponent(i*_height+point%_height);
                str+=temp.getText();
            }
            w[0][point%_height]=str;
            str="";
            int tp=(int)Math.floor(point/_height)*_height;
            for(int i=tp;i<_height+tp;i++)//lấy cho hàng
            {
                temp=(JTextField)pnlM.getComponent(i);
                str+=temp.getText();
            }
            w[1][point/_height]=str;
        }
    }

    private void txtKeyPressed(java.awt.event.KeyEvent evt) {
        // TODO add your handling code here:
        JTextField temp=(JTextField)   evt.getComponent();
        tempStr=temp.getText();
    }

    private void txtFocusGained(java.awt.event.FocusEvent evt) {
        // TODO add your handling code here:
        JTextField temp=(JTextField)   evt.getComponent();
        temp.setSelectionStart(0);
        temp.setSelectionEnd(1);
    }
    private void txtMouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        JTextField temp=(JTextField)   evt.getComponent();
        temp.setSelectionStart(0);
        temp.setSelectionEnd(1);
    }
    JTextField CreateTextField()
    {
        JTextField temp=new JTextField();
        temp.setColumns(2);
        temp.setText("*");
        temp.setSize(27, 19);
        temp.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        temp.addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtFocusGained(evt);
            }
        });
        temp.addKeyListener(new java.awt.event.KeyAdapter() {
            @Override
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtKeyPressed(evt);
            }
            @Override
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtKeyReleased(evt);
            }
        });
        temp.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtMouseClicked(evt);
            }
        });
        return temp;
    }
    JTextField CreateTF(String str)
    {
        JTextField temp=new JTextField();
        temp.setText(str);
        temp.setSize(27, 19);
        temp.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        temp.setEditable(false);
        //temp.setBorder(null);
        return temp;
    }
    void LoadAll(int width,int height)
    {
        pnlM.removeAll();
        pnlIdH.removeAll();
        pnlIdV.removeAll();
        _width=width;
        _height=height;
        JTextField temp=null;
        for(int i=0;i<_width;i++)
        {
            for(int j=0;j<_height;j++)
            {
                temp=CreateTextField();
                temp.setName("txt"+(i*_height+j));
                temp.setLocation(j*distanceW+magin, i*distanceH+magin);
                temp.setVisible(true);
                pnlM.add(temp);
            }
        }
        for(int i=0;i<_height;i++)
        {
            temp =CreateTF(String.valueOf(i+1));
            temp.setSize(27, 19);
            temp.setLocation(magin+i*(magin+distanceH),0);
            temp.setColumns(2);
            pnlIdV.add(temp);
        }
        for(int i=0;i<_width;i++)//số cột.
        {       
            Character c=new Character((char)(65+i%26));
            if(Math.floor(i/26)>0)
                tempStr=c.toString()+Math.round(i/26);
            else
                tempStr=c.toString();
            temp =CreateTF(tempStr);
            temp.setLocation(0, i*distanceH+magin);  
            temp.setSize(27, 19);       
            pnlIdH.add(temp);
        }
        jScrollPane1.validate();
        pnlM.setSize(new Dimension(distanceH*_height+10,distanceW*(_width)+10));
        pnl6.setSize(new Dimension(distanceH*_height+10+pnlIdV.getHeight()+20,distanceW*_width+10+pnlIdH.getWidth()+20));
        pnl6.setPreferredSize(new Dimension(distanceH*_height+10+pnlIdV.getHeight()+30,distanceW*_width+10+pnlIdH.getWidth()+30));
        pnlM.validate();
        changeDimention();
    }
    void changeDimention()
    {
        pnlM.setSize(distanceW*_height+10,distanceH*_width+10);
        pnlM.validate();
        pnlIdH.setSize(pnlIdH.getWidth(),pnlM.getHeight());
        pnlIdV.setSize(pnlM.getWidth(),pnlIdV.getHeight());
        pnlIdH.validate();
        pnlIdV.validate();
    }
    private void btnNewGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewGameActionPerformed
        try {
            // TODO add your handling code here
            dlgType dlg = new dlgType(this, true);
            dlg.setVisible(true);
            if(JOptionPane.showConfirmDialog(this, "Bạn có muốn chơi game này?","",JOptionPane.YES_NO_OPTION)==JOptionPane.NO_OPTION)
                dlg.setType("-1");
            if(dlg.getType().equals("-1"))
                return;
            ID=Integer.parseInt(dlg.getType());
            ResultSet rs = DB.GetInfoGame(dlg.getType(),0);
            rs.next();//GEN-LAST:event_btnNewGameActionPerformed
            lblTitle.setText(rs.getString("title"));
            Date d=new Date();
            lblStartTime.setText(d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());
            t1.start();
            _width=rs.getInt("width");
            _height=rs.getInt("height");
            LoadAll(_width, _height);
            lblType.setText(_width+"X"+_height);
            rs.close();
            rs=DB.GetInfoGame(dlg.getType(), 1);
            String temp="";
            lblGY.setText("Gợi ý:");
            int i=0,n=(_width>_height)?_width:_height;
            w=new String[2][n];
            for(i=0;i<n;i++)
            {
                temp+="*";
            }
            for(i=0;i<n;i++)
            {
                w[0][i]=temp.substring(0,_width);
                w[1][i]=temp.substring(0,_height);
            }
            i=0;
            temp="Gợi ý cho hàng\n";
            String temp1="";
            while(rs.next())
            {
                i=rs.getInt("point");
                String str=rs.getString("Answer");
                String title=rs.getString("title");
                
                if(i<_width)
                    w[0][i]=str;
                else
                    w[1][i-_width]=str;
                if(i<_width)
                {
                    Character c=new Character((char)(65+i%26));
                    if(Math.floor(i/26)>0)
                    {
                        tempStr=c.toString()+Math.round(i/26);
                    }
                    else
                    {
                        tempStr=c.toString();
                    }
                    temp+="Hàng "+tempStr+":  "+title+"("+replateStr(str).length()+" chữ cái)\n";
                }
                else
                    temp1+="Cột thứ "+(i-_width+1)+":  "+title+"("+replateStr(str).length()+" chữ cái)\n";
            }
            txtMain.setText(temp+"\n\n\nGợi ý cho cột: \n"+temp1);
            i=0;
            JTextField txt=null;
            for(i=0;i<_width;i++)
            {
                temp=w[0][i];
                for(int j=0;j<_height;j++)
                {
                    txt=(JTextField) pnlM.getComponent(i*_height+j);
                    if(String.valueOf(temp.charAt(j)).equals("*"))
                        txt.setEditable(false);
                }
            }
            btnNewGame.setEnabled(false);
            btnEndGame.setEnabled(true);
            txtTime.setText("00:00:00 Phút");
            btnPublic.setEnabled(false);
        } catch (SQLException ex) {
            Logger.getLogger(frmMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void mnuAddQActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuAddQActionPerformed
        // TODO add your handling code here:
        dlgLogin lg=null;
        if(!Login)
        {
            lg=new dlgLogin(this,true);
            lg.setVisible(true);
        }
        if(!lg.isLogin())
            return;
        frmAddQ f=new frmAddQ();
        f.setVisible(true);
    }//GEN-LAST:event_mnuAddQActionPerformed

    private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenu1ActionPerformed
    String replateStr(String str)
    {
        String c="";
        for(int i=0;i<str.length();i++)
            if(str.charAt(i)!='*')
                c+=str.substring(i,i+1);
        return c;
    }
    String getString(int type,int number)
    {
        JTextField temp=null;
        String str="";
        if(type==1)//cột
        {
            for(int i=0;i<_width;i++)//lấy cho cột
            {
                temp=(JTextField)pnlM.getComponent(i*_height+number);
                str+=temp.getText();
            }
            return str;
        }
        else
        {
            for(int i=number*_height;i<number*_height+_height;i++)//lấy cho hàng
            {
                temp=(JTextField)pnlM.getComponent(i);
                str+=temp.getText();
            }
            return str;
        }
        
    }
    private void btnEndGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEndGameActionPerformed
        // TODO add your handling code here:
        if(JOptionPane.showConfirmDialog(this, "Bạn muốn kết thúc.","",JOptionPane.YES_NO_OPTION)==JOptionPane.NO_OPTION)
            return;
        t1.stop();
        for(int i=0;i<pnlM.getComponentCount();i++)
        {
            JTextField txt=(JTextField)pnlM.getComponent(i);
            txt.setEditable(false);
        }
        String str="Hàng:\n";
        int count=0;
        try {
            ResultSet rs = DB.GetInfoGame(String.valueOf(ID), 1);
            int point,total=0;
            String title="",da="",temp1="\n\nCột:\n";
            while (rs.next()) {
                total++;
                point=rs.getInt("point");
                title=rs.getString("title");
                da=rs.getString("answer");
                if(point<_width)
                {
                    Character c=new Character((char)(65+point%26));
                    if(Math.floor(point/26)>0)
                    {

                        tempStr=c.toString()+Math.round(point/26);
                    }
                    else
                    {
                        tempStr=c.toString();
                    }
                    if(getString(0, point).equalsIgnoreCase(da))
                    {
                        count++;
                        str+="\n Hàng "+tempStr+":  "+title+"\n\t Bạn chọn: "+replateStr(getString(0, point))+"\t Đáp án: "+replateStr(da)+"\t Đúng.";
                    }
                    else
                        str+="\n Hàng "+tempStr+":  "+title+"\n\t Bạn chọn: "+replateStr(getString(0, point))+"\t Đáp án: "+replateStr(da)+"\t Sai.";
                    
                }
                else
                {
                    if(getString(1, point-_width).equalsIgnoreCase(da))
                    {
                        count++;
                        temp1+="\n Cột thứ "+(point-_width+1)+":  "+title+"\n\t Bạn chọn: "+replateStr(getString(1, point-_width))+"\t Đáp án: "+replateStr(da)+"\t Đúng.";
                    }
                    else
                        temp1+="\n Cột thứ "+(point-_width+1)+":  "+title+"\n\t Bạn chọn: "+replateStr(getString(1, point-_width))+"\t Đáp án: "+replateStr(da)+"\t Sai.";
                }
                
        }
            txtMain.setText(str+temp1+"\n Bạn trả lời đúng "+count+" câu.");
            txtMain.setSelectionStart(1);
            btnEndGame.setEnabled(false);
            lblGY.setText("Kết quả:");
            StartTime=0;
            btnPublic.setEnabled(true);
            mark=Integer.valueOf( count/total *10+(300-StartTime)/300 *5);
    } catch (SQLException ex) {
        Logger.getLogger(frmMain.class.getName()).log(Level.SEVERE, null, ex);
    }
        btnNewGame.setEnabled(true);        
}//GEN-LAST:event_btnEndGameActionPerformed

    private void mnuAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuAboutActionPerformed
        // TODO add your handling code here:
        dlgAbout lg=new dlgAbout(this,true);
        lg.setVisible(true);
    }//GEN-LAST:event_mnuAboutActionPerformed

    private void btnPublicActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPublicActionPerformed
        // TODO add your handling code here:
        dlgGetName f=new dlgGetName(this, true);
        f.setVisible(true);
        if(f.name.equals(""))
            return;
        Date now = new Date();
        DateFormat df =  DateFormat.getDateInstance();
        String str=df.format(now);
        name=f.name;
        DB.SetHS(name, mark, str.substring(str.length()-10), txtTime.getText(), _width+"X"+_height);
        btnPublic.setEnabled(false);
    }//GEN-LAST:event_btnPublicActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        dlgHighScore f=new dlgHighScore(this, true);
        f.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_jMenuItem2ActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmMain().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEndGame;
    private javax.swing.JButton btnNewGame;
    private javax.swing.JButton btnPublic;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lblGY;
    private javax.swing.JLabel lblStartTime;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JLabel lblType;
    private javax.swing.JMenuItem mnuAbout;
    private javax.swing.JMenuItem mnuAddQ;
    private javax.swing.JPanel pnl6;
    private javax.swing.JPanel pnlIdH;
    private javax.swing.JPanel pnlIdV;
    private javax.swing.JPanel pnlM;
    private javax.swing.JPanel pnlTime;
    private javax.swing.JTextArea txtMain;
    private javax.swing.JTextField txtTime;
    // End of variables declaration//GEN-END:variables
    
}
